package com.zhao.dota.controller;

import com.zhao.dota.bo.AuthenticateUser;
import com.zhao.dota.security.SecurityUtil;
import com.zhao.dota.security.token.AccessToken;
import com.zhao.dota.service.UserService;
import com.zhao.dota.struct.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/mine")
public class MineController {

    @Resource
    private UserService userService;

    @GetMapping("/getUserInfo")
    public Result<AuthenticateUser> getUserInfo() {
        AuthenticateUser authenticateUser = SecurityUtil.getAuthenticateUser();
        return Result.success(authenticateUser);
    }

    @PostMapping("/uploadUserInfo")
    public Result<AccessToken> uploadWechatUserInfo(String nickName, String avatarUrl, String phone, int gender) {
        AccessToken accessToken = userService.saveWechatUserInfo(nickName, avatarUrl, phone, gender);
        return Result.success(accessToken);
    }

    @PostMapping("/updateInfo")
    public Result<AccessToken> updateInfo(String nickName, String avatar, String phone, int gender) {
        AccessToken accessToken = userService.saveUserInfo(nickName, avatar, phone, gender);
        return Result.success(accessToken);
    }

    @PostMapping("/changeUsername")
    public Result<Void> changeUsername(String username) {
        userService.changeUsername(username.trim());
        return Result.success();
    }

    @PostMapping("/changePassword")
    public Result<Void> changePassword(String original, String changed) {
        boolean success = userService.changePassword(original, changed);
        return Result.complete(success);
    }

}
